import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/workItem'

const api = {

    /**
     * 新增节点任务实例
     * @param workItem
     * @returns {Promise<*|undefined>}
     */
    createWorkItem: (workItem) =>
      axiosClient().post(`${requestUrl}/addInfo`, workItem),

    /**
     * 根据ID删除节点任务实例
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteWorkItemById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新节点任务实例
     * @param id
     * @param workItem
     * @returns {Promise<*|undefined>}
     */
    updateWorkItemFull: (id, workItem) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, workItem),

    /**
     * 根据ID部分更新节点任务实例
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateWorkItemPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询节点任务实例
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getWorkItemById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询节点任务实例
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getWorkItemPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询节点任务实例
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getWorkItemList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.instanceId) {
            params.append('instanceId', businessParam.instanceId)
        }
        if (businessParam.nodeId) {
            params.append('nodeId', businessParam.nodeId)
        }
        if (businessParam.nodeCode) {
            params.append('nodeCode', businessParam.nodeCode)
        }
        if (businessParam.nodeType) {
            params.append('nodeType', businessParam.nodeType)
        }
        if (businessParam.handlerId) {
            params.append('handlerId', businessParam.handlerId)
        }
        if (businessParam.handlerName) {
            params.append('handlerName', businessParam.handlerName)
        }
        if (businessParam.workItemStatus) {
            params.append('workItemStatus', businessParam.workItemStatus)
        }
        if (businessParam.viewPath) {
            params.append('viewPath', businessParam.viewPath)
        }
        if (businessParam.startTime) {
            params.append('startTime', businessParam.startTime)
        }
        if (businessParam.completeTime) {
            params.append('completeTime', businessParam.completeTime)
        }
        if (businessParam.opinion) {
            params.append('opinion', businessParam.opinion)
        }
        if (businessParam.decision) {
            params.append('decision', businessParam.decision)
        }
        if (businessParam.prevNodeId) {
            params.append('prevNodeId', businessParam.prevNodeId)
        }
        if (businessParam.prevWorkItemId) {
            params.append('prevWorkItemId', businessParam.prevWorkItemId)
        }
        if (businessParam.prevHandlerId) {
            params.append('prevHandlerId', businessParam.prevHandlerId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
